<?php

namespace app\index\controller;

use tian\Session;

class Works extends Base
{
    public function refine()
    {
        try {
            $role_id = Session::get('uid');

            $id = input('id');
            $infoEquip = model('RoleEquip')->getInfo($id);
            $this->assign('infoEquip', $infoEquip);

            //成功率
            $rate = get_strengthen($infoEquip['qianghua']);
            $this->assign('rate', $rate);

            //消耗
            $stuff = get_strengthen_stuff($infoEquip['qianghua'], $infoEquip['zblv']);
            $this->assign('stuff', $stuff);

            //拥有
            $mapEquip = [];
            $mapEquip[] = ['uid', '=', $role_id];
            $mapEquip[] = ['djzl', '=', 2];
            $mapEquip[] = ['djid', '=', 1];
            $myStone = model('RoleProp')->findValue($mapEquip, 'djsum');
            $this->assign('myStone', $myStone);

            $this->fetch('refine');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function refineAct()
    {
        try {
            $role_id = Session::get('uid');

            $id = input('id');
            $infoEquip = model('RoleEquip')->getInfo($id);

            //拥有道具
            $mapEquip = [];
            $mapEquip[] = ['uid', '=', $role_id];
            $mapEquip[] = ['djzl', '=', 2];
            $mapEquip[] = ['djid', '=', 1];
            $myRoleProp = model('RoleProp')->getWhereInfo($mapEquip);
            $myStone = $myRoleProp['djsum'];

            //消耗道具
            $stuff = get_strengthen_stuff($infoEquip['qianghua'], $infoEquip['zblv']);

            if ($stuff > $myStone) {
                returnJson(0, '道具不足');
            }

            //扣除道具
            $tempRoleProp = [
                'id' => $myRoleProp['id'],
                'djsum' => $myRoleProp['djsum'] - $stuff,
            ];
            model('RoleProp')->saved($tempRoleProp);

            //结果验证
            $iStatus = get_strengthen_status($infoEquip['qianghua']);
            if ($iStatus) {
                //获取波动值
                $wave = get_average_wave($infoEquip['qianghua'], 20);

                //基础值
                $temp = [
                    'zbnowid' => $infoEquip['zbnowid'],
                    'zbgj' => ceil($infoEquip['zbgj'] * (1 + $wave)),
                    'zbfy' => ceil($infoEquip['zbfy'] * (1 + $wave)),
                    'zbbj' => ceil($infoEquip['zbbj'] * (1 + $wave)),
                    'zbxx' => ceil($infoEquip['zbxx'] * (1 + $wave)),
                    'zbsd' => ceil($infoEquip['zbsd'] * (1 + $wave)),
                    'qianghua' => $infoEquip['qianghua'] + 1,
                ];
                model('RoleEquip')->saved($temp);

                returnJson(1, '成功！恭喜你。');
            } else {
                returnJson(0, '失败！请再接再厉。');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

}
